/*
 * Copyright (C) 2018 guomw.Inc. All rights reserved.
 * 项目名称： report-manage
 * 文件名称： AppInterceptor.java
 * Date：18-11-5 上午11:10
 * Author: guomw
 */

package com.guomw.report.web.config;

import com.alibaba.fastjson.JSON;
import com.guomw.report.common.config.Constant;
import com.guomw.report.common.enums.AppCode;
import com.guomw.report.common.utils.ApiResult;
import com.guomw.report.common.utils.CompareUtils;
import com.guomw.report.common.utils.RequestUtils;
import com.guomw.report.common.utils.SignatureUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * @author guomw
 * @date 2018/9/11
 */
public class AppInterceptor implements HandlerInterceptor {

    private static Log log = LogFactory.getLog(AppInterceptor.class);
    @Autowired
    private Environment env;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        if (env.acceptsProfiles(Constant.PROFILE_DEV)) {
            return true;
        }
        /**
         * 检查 固定参数是否没传，没传指的是没有这个key
         */
        if (!checkMustParameter(request, response)) {
            return false;
        }

        Long timestamp = RequestUtils.getLongParameter(request, Constant.APP_TIMESTAMP_KEY);
        /**
         * 验证时间戳是否是在有效时间内
         */
        if (timestamp == null || !CompareUtils.compareToTimestamp(timestamp, Constant.TIMESTAMP_VALID_TIME_LENGTH)) {
            response.getWriter().write(JSON.toJSONString(ApiResult.resultWith(AppCode.TIMESTAMP_ERROR)));
            return false;
        }

        String requestSign = RequestUtils.getParameter(request, Constant.SIGN_KEY);
        //签名未传
        if (StringUtils.isEmpty(requestSign)) {
            response.getWriter().write(JSON.toJSONString(ApiResult.resultWith(AppCode.SIGN_NOT_PASS)));
            return false;
        }
        if (!requestSign.equalsIgnoreCase(SignatureUtils.getSign(request))) {
            response.getWriter().write(JSON.toJSONString(ApiResult.resultWith(AppCode.SIGN_ERROR)));
            return false;
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

    /**
     * 检查必要的参数是否存
     *
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    private boolean checkMustParameter(HttpServletRequest request, HttpServletResponse response) throws IOException {
        /**
         * 检查 参数是否没传，没传指的是没有这个key
         */
        boolean result = true;
        return result;
    }

}
